<?php

class SCCacheHelper {
    private static $_Instance = null;
    
    /**
     *
     * @return SCCacheHelper
     */
    public static function getInstance() {
        if (self::$_Instance===null) {
            self::$_Instance = new SCCacheHelper();
            self::$_Instance->getMemcache();
        }
        return self::$_Instance;
    }
    
    private $_Memcache = null;

    /**
     * 
     * @return Memcache
     */
    public function getMemcache() {
        if ($this->_Memcache === null) {
            if (class_exists("Memcache")) {
                $this->_Memcache = new Memcache;
                if ($this->_Memcache->connect('localhost', 11211) !== true) {
                    $this->_Memcache = false;
                }
            } else {
                $this->_Memcache = false;
            }
        }
        return $this->_Memcache;
    }

    private $_IsAvailable;

    public function isAvailable() {
        if ($this->_IsAvailable === null) {
            $this->_IsAvailable = self::getInstance()->getMemcache() !== false;
        }
        return $this->_IsAvailable;
    }
    
    public function getObject($name) {
        return IS_DEV ? false : $this->_Memcache->get($name);
        return $this->isAvailable() ? $this->getMemcache()->get($name) : false;
    }

    public function setObject($name, $value) {
        if ($this->isAvailable())
            $this->getMemcache()->set($name, $value);
        return $value;
    }

    public function flush()
    {
        $this->getMemcache()->flush();
    }
}